require.config({
    paths: {
        AJAXPM: "./AJAXPM"
    },
    shim: {
        AJAXPM: { exports: 'AJAXPM' },
    }
});

require(
    ["AJAXPM"],
    function(AJAXPM) {
        const doms = {
            idCodeEl: document.getElementById("idCode"),
            passwordEl: document.getElementById("password"),
            password2El: document.getElementById("password2"),
            toRegisterEl: document.getElementById("toRegister"),
            registerEl: document.getElementById("register"),
            loginEl: document.getElementById("login"),
        }

        const loginAction = result => {
            let { code, msg, data } = JSON.parse(result);
            switch (code) {
                case "01":
                    // 跳转主页
                    localStorage.setItem("loginAdmin", JSON.stringify(data));
                    window.open("/home.html", "_self");
                    break;
                case "02":
                    alert("用户名不存在");
                    break;
                case "03":
                    alert("密码错误");
                    break;
                default:
                    alert("请求失败");
            }
        }

        const registerAction = result => {
            let { code, msg, data } = JSON.parse(result);
            switch (code) {
                case "01":
                    window.open("/home.html", "_self");
                    break;
                case "02":
                    alert("用户名占用");
                    break;
                default:
                    alert("请求失败");
            }
        }

        const domEvent = () => {
            // doms.verCodeIEl.addEventListener("click", () => {
            //     AJAXP.GET("/getVerCode", [], (result = "验证码") => {
            //         doms.verCodeIEl.innerText = result;
            //     });
            // });

            doms.loginEl.addEventListener("click", () => {
                let loginUser = {
                    username: doms.idCodeEl.value,
                    password: doms.passwordEl.value,
                    // verCode: doms.verCodeEl.value,
                    // verCodeI: doms.verCodeIEl.innerText,
                }

                if (loginUser.username === "" || loginUser.password === "")
                    return alert("账户和密码不能为空!");

                // if (loginUser.verCode !== loginUser.verCodeI)
                //     return alert("验证码错误!");
                // delete loginUser.verCode;
                // delete loginUser.verCodeI;

                AJAXPM.POST("/loginAdmin", loginUser, {}, loginAction);
            });

            doms.toRegisterEl.addEventListener("click", () => {
                doms.password2El.style.display = "block";
                doms.loginEl.style.display = "none";
                doms.toRegisterEl.style.opacity = "0";
                doms.registerEl.style.display = "inline-block";
            });

            doms.registerEl.addEventListener("click", () => {
                let registerUser = {
                    username: doms.idCodeEl.value,
                    password: doms.passwordEl.value,
                    password2: doms.password2El.value,
                }

                if (registerUser.username === "" || registerUser.password === "")
                    return alert("账户和密码不能为空!");
                if (registerUser.password !== registerUser.password2)
                    return alert("确认密码正确性!");

                delete registerUser.password2;

                AJAXPM.POST("/registerAdmin", registerUser, {}, registerAction);

            });

        }
        domEvent();

    }
);